package com.smartlifev30.baiwei.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.smartlifev30.R;
import com.smartlifev30.baiwei.activity.ControlActivity;
import com.smartlifev30.baiwei.activity.DoorLockActivity;
import com.smartlifev30.baiwei.activity.LinkageActivity;
import com.smartlifev30.baiwei.activity.SceneActivity;
import com.smartlifev30.baiwei.activity.TimerActivity;
import com.smartlifev30.baiwei.activity.ZoneActivity;
import com.smartlifev30.baiwei.adapter.HomeContentAdapter;
import com.smartlifev30.baiwei.bean.HomeContentItem;
import com.smartlifev30.baiwei.view.banner.BannerView;
import com.smartlifev30.baiwei.view.recyclerViewComponent.GridItemDivider;
import com.smartlifev30.mvp.presenter.BasePresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author tys
 * @date 2018/8/15
 */

public class HomeFragment extends BaseFragment {
    private int[] contentImgs = {
            R.drawable.app_control, R.drawable.app_scene, R.drawable.app_linkage,
            R.drawable.app_timer, R.drawable.app_zone, R.drawable.app_door,
            R.drawable.app_environment, R.drawable.app_video, R.drawable.peep_hole_logo_80,
            R.drawable.weiju_icon};

    private int[] bannerImgs = {R.drawable.view_add_1, R.drawable.view_add_2,
            R.drawable.view_add_3, R.drawable.view_add_4};

    @BindView(R.id.home_recycler_view)
    RecyclerView contentRv;

    @BindView(R.id.home_bran_view)
    BannerView banner;
    private HomeContentAdapter homeContentAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View homeView = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, homeView);
        initView();
        return homeView;
    }

    private void initView() {
        contentRv.setLayoutManager(new GridLayoutManager(getContext(), 3));
        homeContentAdapter = new HomeContentAdapter(getContext(), new ArrayList<>());
        String[] descriptions = getResources().getStringArray(R.array.home_options);
        int index = 0;
        for (String description : descriptions) {
            homeContentAdapter.addData(new HomeContentItem(description, contentImgs[index++]));
        }

        homeContentAdapter.setItemClickListen((itemView, position) -> {
            HomeContentItem data = homeContentAdapter.getData(position);
            switch (data.getDrawableResId()) {
                //智能控制
                case R.drawable.app_control:
                    toControl();
                    break;
                //情景模式
                case R.drawable.app_scene:
                    toScene();
                    break;
                //联动
                case R.drawable.app_linkage:
                    toLinkage();
                    break;
                //定时
                case R.drawable.app_timer:
                    toTimer();
                    break;
                //家庭安防
                case R.drawable.app_zone:
                    toZone();
                    break;
                //门锁
                case R.drawable.app_door:
                    toDoor();
                    break;
                //居家环境
                case R.drawable.app_environment:
                    break;
                //视频看家
                case R.drawable.app_video:
                    break;
                //猫眼
                case R.drawable.peep_hole_logo_80:
                    break;
                //楼宇对讲
                case R.drawable.weiju_icon:
                    break;
                default:
            }
            Toast.makeText(getContext(), homeContentAdapter.getData(position).getDes(), Toast.LENGTH_SHORT).show();
        });
        contentRv.addItemDecoration(new GridItemDivider(getContext()));
        contentRv.setAdapter(homeContentAdapter);
        banner.setImageResource(bannerImgs);
        banner.setOnBannerClickListen(index1 -> {
            Toast.makeText(getContext(), String.valueOf(index1), Toast.LENGTH_SHORT).show();
        });
    }

    private void toZone() {
        Intent intent = new Intent(getContext(), ZoneActivity.class);
        startActivity(intent);
    }

    private void toTimer() {
        Intent intent = new Intent(getContext(), TimerActivity.class);
        startActivity(intent);
    }

    private void toLinkage() {
        Intent intent = new Intent(getContext(), LinkageActivity.class);
        startActivity(intent);
    }

    private void toDoor() {
        Intent intent = new Intent(getContext(), DoorLockActivity.class);
        startActivity(intent);
    }

    private void toScene() {
        Intent intent = new Intent(getContext(), SceneActivity.class);
        startActivity(intent);
    }

    private void toControl() {
        Intent intent = new Intent(getContext(), ControlActivity.class);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.startBanner();
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.stopBanner();
    }

    @Override
    protected BasePresenter createPresent() {
        return null;
    }


    @OnClick(R.id.home_voice_control)
    void voiceControl() {
        Toast.makeText(getContext(), "声音", Toast.LENGTH_SHORT).show();
    }
}
